package org.josircd.config.manager;

import org.josircd.config.bean.IrcdConfiguration;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.File;

/**
 * org.josircd.config.manager
 *
 * @author richardju
 * @version 1.0
 */
public class ConfigManager {
    /** */
    private static final String IRCD_CONFIG_FILE = "./config/josircd.xml";
    /** */
    private IrcdConfiguration config;

    public ConfigManager() {
        loadConfig();
    }

    public void refresh() {
        loadConfig();
    }

    private void loadConfig() {
        JAXBContext jaxbContext;
        try {
            jaxbContext = JAXBContext.newInstance(IrcdConfiguration.class);
            Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();
            setConfig((IrcdConfiguration) unMarshaller.unmarshal(new File(IRCD_CONFIG_FILE)));
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }

    public IrcdConfiguration getConfig() {
        return config;
    }

    public void setConfig(IrcdConfiguration config) {
        this.config = config;
    }
}
